<template>
  <div class="form-crud-container">
    <el-form
      :size="form.size"
      :model="form.data"
      :rules="form.batchUpdate ? null : form.rules"
      :label-width="form.labelWidth + 'px'"
      :label-position="form.labelPosition"
      ref="crudForm">
      <el-row
        :gutter="12">
        <el-col
          v-if="form.type === '1'"
          :span="12">
          <el-form-item
            :label-width="form.list[0].plugin.labelWidth + 'px'"
            prop="parentId" label="父级ID">
            <select-tree
              size=""
              :value="form.parentId"
              :data="form.table"
              :props="form.props"
              :clearable="true"
              :accordion="true"
              @getValue="(value) => {form.parentId = value}"/>
          </el-form-item>
        </el-col>
        <template v-for="(item, index) in form.list">
          <el-col
            :span="item.layout"
            :key="'col_form.' + index">
            <el-form-item
              :key="'col_form_item.' + index"
              :label-width="item.plugin.labelWidth + 'px'"
              :label="item.hideLabel ?  '' : item.label"
              v-if="item.label === '交付状态' || !form.batchInTurn"
              :prop="form.batchUpdate ? null : item.prop">
              <component :key="'crud_component_item.' + index" :is="'xl-' + item.type" :form="form"
                         :dictionary="dictionary" :item="item"/>
            </el-form-item>
          </el-col>
        </template>
        <el-col :span="24">
          <el-form-item style="text-align: right;">
            <el-button size="medium" type="primary" @click="submitForm">确 定</el-button>
            <el-button size="medium" @click="closeForm">取 消</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import selectTree from '@/components/TreeSelect';
import xlSelect from "./plugin/xl-select";
import xlInput from "./plugin/xl-input";
import xlTextarea from "./plugin/xl-textarea";
import xlDate from "./plugin/xl-date";
import xlTime from "./plugin/xl-time";
import xlInputNumber from "./plugin/xl-inputNumber";

export default {
  name: "FormCrud",
  components: {
    selectTree,
    xlSelect,
    xlDate,
    xlInputNumber,
    xlTime,
    xlInput,
    xlTextarea
  },
  props: {
    dictionary: {
      type: Object,
      default: {}
    },
    form: {
      type: Object,
      default() {
        return {
          parentId: "",
          list: [],
          rules: {},
          childTable: [],
          props: {},
          labelPosition: "top",
          labelWidth: ""
        }
      }
    }
  },
  data() {
    return {}
  },
  methods: {
    closeForm() {
      this.resetForm("crudForm");
      this.$emit("close");
    },
    submitForm() {
      if (this.form.batchUpdate) {
        this.$emit("submit", this.form);
      } else {
        this.$refs['crudForm'].validate(valid => {
          if (valid) {
            this.$emit("submit", this.form);
          } else {
            this.$notify.info("数据校验不通过");
          }
        })
      }
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep.el-form--label-top .el-form-item__label {
  padding: 0;
}
</style>
